package cn.itsource.service.impl;

import cn.itsource.annotation.Logs;
import cn.itsource.domain.Role;
import cn.itsource.domain.Menu;
import cn.itsource.domain.Permission;
import cn.itsource.domain.Role;
import cn.itsource.mapper.RoleMapper;
import cn.itsource.query.RoleQuery;
import cn.itsource.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Logs
    @Transactional
    @Override
    public void add(Role role) {
        roleMapper.add(role);
        mongoTemplate.save(role);
        if (role.getMypermissions().size()>0){//前端至少选择了一个权限
            roleMapper.addPermissions(role);
        }
        if (role.getMymenus().size()>0){//前端至少选择了一个菜单
            roleMapper.addMenus(role);
        }

    }
    @Logs
    @Transactional
    @Override
    public void update(Role role) {
        roleMapper.update(role);
        //删除菜单
        roleMapper.delPermissionByRoleId(role.getId());
        //删除权限
        roleMapper.delMenuByRoleId(role.getId());

        if (role.getMypermissions().size()>0){//前端至少选择了一个权限
            roleMapper.addPermissions(role);
        }
        if (role.getMymenus().size()>0){//前端至少选择了一个菜单
            roleMapper.addMenus(role);
        }
    }
    @Logs
    @Transactional
    @Override
    public void del(Long id) {
        //删除菜单
        roleMapper.delPermissionByRoleId(id);
        //删除权限
        roleMapper.delMenuByRoleId(id);

        roleMapper.del(id); //删除角色
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, Role.class);
    }
    @Logs
    @Transactional
    @Override
    public void delList(List<Long> ids) {
        for (Long id : ids) {
            //删除菜单
            roleMapper.delPermissionByRoleId(id);
            //删除权限
            roleMapper.delMenuByRoleId(id);
            roleMapper.del(id);
        }
    }

    @Override
    public Role get(Long id) {
        return roleMapper.get(id);
    }
    @Override
    public PageInfo<Role> pageQuery(RoleQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Role> roles = roleMapper.pageQuery(query);
        for (Role role : roles) {
            List<Permission> permissionsList = roleMapper.findpermissionsByRoleId(role.getId());

            List<String> permissions = new ArrayList<>();
            List<Long> myPermissions = new ArrayList<>();

            for (Permission permission : permissionsList) {
                permissions.add(permission.getName());
                myPermissions.add(permission.getId());
            }
            role.setPermissions(permissions);
            role.setMypermissions(myPermissions);

            List<Menu> menusList = roleMapper.findmenusByRoleId(role.getId());
            List<String> menus = new ArrayList<>();
            List<Long> myMenus = new ArrayList<>();
            for (Menu menu : menusList) {
                menus.add(menu.getName());
                myMenus.add(menu.getId());
            }
            role.setMenus(menus);
            role.setMymenus(myMenus);
        }

        return new PageInfo<>(roles);
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.getAll();
    }
}
